package com.controller;

import com.domain.VideoCollection;
import com.dto.JsonResponse;
import com.service.impl.VideoCollectionServiceImpl;
import com.support.UserSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/videoCollection")
@RestController
public class VideoCollectionApi {

    @Autowired
    private VideoCollectionServiceImpl videoService;

    @Autowired
    private UserSupport userSupport;

    /**
     * 查询视频当前被多少用户收藏
     */
    @GetMapping("/getVideoCollection")
    public JsonResponse<Map<String, Object>> getVideoCollection(@RequestParam Long videoId) {
        Long userId = null;
        try {
            userId = userSupport.getCurrentUserId();
        } catch (Exception ignored) {
        }
        Map<String, Object> result = videoService.getVideoCollections(videoId, userId);
        return new JsonResponse<>(result);
    }

    /**
     * 收藏视频
     */
    @PostMapping("/addVideoCollection")
    public JsonResponse<String> addVideoCollection(@RequestBody VideoCollection videoCollection) {
        Long userId = userSupport.getCurrentUserId();
        videoService.addVideoCollection(videoCollection, userId);
        return JsonResponse.success();
    }

    /**
     * 取消收藏视频
     */
    @GetMapping("/deleteVideoCollection")
    public JsonResponse<String> deleteVideoCollection(@RequestParam Long videoId) {
        Long userId = userSupport.getCurrentUserId();
        videoService.deleteVideoCollection(videoId, userId);
        return JsonResponse.success();
    }

}

